package vn.evolus.pencil.service.impl;

import java.util.List;

import org.springframework.transaction.annotation.Transactional;

import vn.evolus.commons.data.IRepository;
import vn.evolus.pencil.model.Collection;
import vn.evolus.pencil.service.ItemCollectionService;

public class ItemCollectionServiceImpl implements ItemCollectionService {
	private IRepository repository;
	private String uploadFolder;
	
	public ItemCollectionServiceImpl(String uploadFolder, IRepository repository){
		this.repository = repository;
		this.uploadFolder = uploadFolder;
	}
	
	@Override
	@Transactional
	public void saveItemCollection(Collection itemCollection) {
		repository.save(itemCollection);
	}

	@Override
	@Transactional
	public List<Collection> getAllItemCollections() {
		return repository.getAll(Collection.class);
	}
	
	@Override
	@Transactional
	public String getCollectionIcon(long idCollection) {
		String linkIcon = null;
		Collection collection = repository.getById(Collection.class, idCollection);
		
		if (collection == null) return null;
		
		if(collection.getIcon() != null) {
			linkIcon = String.format("%s/%s", this.uploadFolder, collection.getIcon());
		}
		
		return linkIcon;
	}

	@Override
	@Transactional
	public Collection getCollectionById(long idCollection) {
		Collection collection = repository.getById(Collection.class, idCollection);
		return collection;
	}

	@Override
	@Transactional
	public String getCollectionZip(long idCollection) {
		Collection collection = repository.getById(Collection.class, idCollection);
		if (collection == null) return null;
		
		String linkZipFile = String.format("%s/%s", this.uploadFolder, collection.getPackageUrl());
		return linkZipFile;
	}
	
	
}
